<?php

if ( !class_exists('Sepcore_Installer') ) {
	require_once(APPLICATION_PATH . '/application/modules/Sepcore/settings/installer.php');
}

class Tv_Installer extends Sepcore_Installer {

  function _query() {
        $this->_tvBrowsePage();
        $this->_tvViewPage();
        $this->_tvFavouritePage();
    }
    
    

    protected function _tvViewPage() {
        $db = $this->getDb();

        // profile page
        $page_id = $db->select()
                ->from('engine4_core_pages', 'page_id')
                ->where('name = ?', 'tv_tv_view')
                ->limit(1)
                ->query()
                ->fetchColumn();

        if (!$page_id) {
            // Insert page
            $db->insert('engine4_core_pages', array(
                'name' => 'tv_tv_view',
                'displayname' => 'Tv View Page',
                'title' => 'Tv View',
                'description' => 'This page displays tv.',
                'provides' => '',
                'custom' => 0,
            ));
            $page_id = $db->lastInsertId();
            
             // Insert top
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'top',
                'page_id' => $page_id,
                'order' => 1,
            ));
            $top_id = $db->lastInsertId();

            // Insert main
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'main',
                'page_id' => $page_id,
                'order' => 2,
            ));
            $main_id = $db->lastInsertId();

            // Insert top-middle
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'middle',
                'page_id' => $page_id,
                'parent_content_id' => $top_id,
            ));
            $top_middle_id = $db->lastInsertId();
            
           
            // Insert main-middle
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'middle',
                'page_id' => $page_id,
                'parent_content_id' => $main_id,
                'order' => 2,
            ));
            $main_middle_id = $db->lastInsertId();

            // Insert main-right
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'right',
                'page_id' => $page_id,
                'parent_content_id' => $main_id,
                'order' => 1,
            ));
            $main_right_id = $db->lastInsertId();
            
            // Insert menu
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'tv.browse-menu',
                'page_id' => $page_id,
                'parent_content_id' => $top_middle_id,
                'order' => 1,
            ));
            
            // Insert content
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'core.content',
                'page_id' => $page_id,
                'parent_content_id' => $main_middle_id,
                'order' => 1,
            ));
            
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'core.comments',
                'page_id' => $page_id,
                'parent_content_id' => $main_middle_id,
                'order' => 2,
            ));

            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'sepcore.addthis',
                'page_id' => $page_id,
                'parent_content_id' => $main_right_id,
                'order' => 1
            ));

            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'tv.tv-options',
                'page_id' => $page_id,
                'parent_content_id' => $main_right_id,
                'order' => 2
            ));

            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'tv.similar-tv',
                'page_id' => $page_id,
                'parent_content_id' => $main_right_id,
                'order' => 3
            ));
        }

        return $this;
    }

    protected function _tvBrowsePage() {
        $db = $this->getDb();

        $page_id = $db->select()
                ->from('engine4_core_pages', 'page_id')
                ->where('name = ?', 'tv_index_browse')
                ->limit(1)
                ->query()
                ->fetchColumn();

        if (!$page_id) {
            // Insert page
            $db->insert('engine4_core_pages', array(
                'name' => 'tv_index_browse',
                'displayname' => 'Tv Browse Page',
                'title' => 'Tv Browse',
                'description' => 'This page lists tv.',
                'custom' => 0,
            ));
            $page_id = $db->lastInsertId();

            // Insert top
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'top',
                'page_id' => $page_id,
                'order' => 1,
            ));
            $top_id = $db->lastInsertId();

            // Insert main
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'main',
                'page_id' => $page_id,
                'order' => 2,
            ));
            $main_id = $db->lastInsertId();

            // Insert top-middle
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'middle',
                'page_id' => $page_id,
                'parent_content_id' => $top_id,
            ));
            $top_middle_id = $db->lastInsertId();
            
           
            // Insert main-middle
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'middle',
                'page_id' => $page_id,
                'parent_content_id' => $main_id,
                'order' => 2,
            ));
            $main_middle_id = $db->lastInsertId();

            // Insert main-right
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'right',
                'page_id' => $page_id,
                'parent_content_id' => $main_id,
                'order' => 1,
            ));
            $main_right_id = $db->lastInsertId();
            
            // Insert main-left
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'left',
                'page_id' => $page_id,
                'parent_content_id' => $main_id,
                'order' => 1,
            ));
            $main_left_id = $db->lastInsertId();


            // Insert menu
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'tv.browse-menu',
                'page_id' => $page_id,
                'parent_content_id' => $top_middle_id,
                'order' => 1,
            ));

            // Insert content
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'core.content',
                'page_id' => $page_id,
                'parent_content_id' => $main_middle_id,
                'order' => 1,
            ));

            // Insert search
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'tv.browse-search',
                'page_id' => $page_id,
                'parent_content_id' => $main_right_id,
                'order' => 1,
            ));

            // Insert categories-list
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'tv.categories-list',
                'page_id' => $page_id,
                'parent_content_id' => $main_right_id,
                'order' => 2,
            ));

            // Insert featured-tv
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'tv.featured-tv',
                'page_id' => $page_id,
                'parent_content_id' => $main_right_id,
                'order' => 3,
            ));

            // Insert cooming-up
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'tv.cooming-up',
                'page_id' => $page_id,
                'parent_content_id' => $main_right_id,
                'order' => 4,
            ));
            
            //Insert country
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'tv.country',
                'page_id' => $page_id,
                'parent_content_id' => $main_left_id,
                'order' => 1,
            ));
            //Insert language
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'tv.language',
                'page_id' => $page_id,
                'parent_content_id' => $main_left_id,
                'order' => 2,
            ));
        }
    }

    protected function _tvFavouritePage() {
        $db = $this->getDb();

        $page_id = $db->select()
                ->from('engine4_core_pages', 'page_id')
                ->where('name = ?', 'tv_index_favourite')
                ->limit(1)
                ->query()
                ->fetchColumn();

        if (!$page_id) {
            // Insert page
            $db->insert('engine4_core_pages', array(
                'name' => 'tv_index_favourite',
                'displayname' => 'Tv Favourite Page',
                'title' => 'Tv Browse favourite',
                'description' => 'This page lists favourite tv.',
                'custom' => 0,
            ));
            $page_id = $db->lastInsertId();

            // Insert top
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'top',
                'page_id' => $page_id,
                'order' => 1,
            ));
            $top_id = $db->lastInsertId();

            // Insert main
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'main',
                'page_id' => $page_id,
                'order' => 2,
            ));
            $main_id = $db->lastInsertId();

            // Insert top-middle
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'middle',
                'page_id' => $page_id,
                'parent_content_id' => $top_id,
            ));
            $top_middle_id = $db->lastInsertId();

            // Insert main-middle
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'middle',
                'page_id' => $page_id,
                'parent_content_id' => $main_id,
                'order' => 2,
            ));
            $main_middle_id = $db->lastInsertId();

            // Insert main-right
            $db->insert('engine4_core_content', array(
                'type' => 'container',
                'name' => 'right',
                'page_id' => $page_id,
                'parent_content_id' => $main_id,
                'order' => 1,
            ));
            $main_right_id = $db->lastInsertId();


            // Insert menu
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'tv.browse-menu',
                'page_id' => $page_id,
                'parent_content_id' => $top_middle_id,
                'order' => 1,
            ));



            // Insert content
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'core.content',
                'page_id' => $page_id,
                'parent_content_id' => $main_middle_id,
                'order' => 1,
            ));

            // Insert search
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'tv.browse-search',
                'page_id' => $page_id,
                'parent_content_id' => $main_right_id,
                'order' => 1,
            ));

            // Insert popular-tv
            $db->insert('engine4_core_content', array(
                'type' => 'widget',
                'name' => 'tv.popular-tv',
                'page_id' => $page_id,
                'parent_content_id' => $main_right_id,
                'order' => 2,
            ));
        }
    }

}

?>